# coding=utf-8
import xml.dom.minidom as minidom
import re
import os
def parse_public_data():# 读取文件
    data = []
    path = os.path.abspath('public.xml')
    xml = minidom.parse(path)
    dom = xml.documentElement
    elements = dom.getElementsByTagName('public')
    for element in elements:
        item = (element.getAttribute('type'),element.getAttribute('name'),str(int(element.getAttribute('id'),16)))
        data.append(item)
    pass
    return data

def list_files(filepath,data):
#遍历filepath下所有文件，包括子目录
    files = os.listdir(filepath)
    for fi in files:
        fi_d = os.path.join(filepath,fi)
        if os.path.isdir(fi_d):
          list_files(fi_d,data)
        else:
          data.append(os.path.join(filepath,fi_d))

if __name__ == '__main__':
    #[type,name,id]
    resources = parse_public_data()
    print(len(resources))
    files = []
    dir = os.path.abspath('old')
    list_files(dir,files)
    for path in files:
        old_file = open(path,'r',encoding='utf-8')
        lines = old_file.readlines()
        old_file.close()
        new_file = open(path,'w+',encoding='utf-8')
        for line in lines:
            items = re.findall("213[\d]{7}",line)
            if len(items) == 0:
                pass
            else:
                for item in items:
                    for resource in resources:
                        if item == resource[2]:
                            line = line.replace(item,"R.%s.%s"%(resource[0],resource[1]))
            new_file.write(line)
        new_file.close






